<template>
	<view class="evaluate" v-if="status==1">

		<None v-if="list_empty"></None>
		<view class="comment" v-for="(item, index) of commentList" :key="index" v-else>
			<block v-if="currentTab == 0 ">
				<view class='con'>
					<view class='con_1'>
						<view class='con_1_l'>
							<img :src='item.avatar' />
							<view class='name'>{{item.nickname}}<span></span>
								<view class='con_1_r'>{{item.add_time | formatTime}}</view>
							</view>
						</view>
						<view class='con_1_r'>
							<view class="ping">
								<tui-rate :current="item.taste_rate" @change="changedd" active="#CE935B" :disabled="true"></tui-rate>
							</view>
						</view>
					</view>
					<view class='con_2'>
						{{item.content}}
						<view class="img">
							<block v-for="(url,index) of item.imgs" :key="index" v-if="index<9">
								<img :src="getimg + url" @click="ViewImage(item.imgs, index)"></img>
							</block>
						</view>
					</view>

					<view class="reply" v-if="item.reply_content!=null">商家回复：</br>
						<view style="margin-left: 20px;padding: 10px;">
							{{item.reply_content}}
						</view>
					</view>

				</view>
			</block>

			<view class='H10'></view>
		</view>
		<!-- 弹框评论 -->
		<ygc-comment ref="ygcComment" :placeholder="'发布评论'" @pubComment="pubComment"></ygc-comment>
		<view class="fixed-btn">
			<button v-if="isHasOrder" type="primary" class="submit-pl" @tap="showComment">发布评论</button>
		</view>
	</view>
</template>

<script>
	// import {
	// 	Api_url
	// } from '@/common/config'
	import None from "./components/none.vue"
	import tuiTabs from "./components/tui-tabs"
	import tuiRate from "./components/rate"
	import ygcComment from './components/ygc-comment'
	// import productModel from '@/api/product.js'
	// import commentModel from '@/api/comment.js'
	// import orderModel from '@/api/orders.js'
	export default {
		data() {
			return {
				getimg: this.$getimg,
				currentTab: 0,
				navbar: [{
					name: "全部"
				}, {
					name: "有图"
				}],
				shua: true,
				list_empty: false,
				commentList: [],
				current: 5,
				index: 5,
				img: [],
				dishes_id: -1,
				indent_id: -1,
				isHasOrder: true,
				status:3
			};
		},
		components: {
			tuiTabs,
			tuiRate,
			None,
			ygcComment
		},
		onLoad(options) {
			let _this=this;
			// this.getBanner();
			uni.request({
				url: 'https://apiorder.wzq0307.com/api/Status/index',
				method: 'GET',
				data: {},
				success: (res) => {
					console.log(11111,res.data.data.status);
					_this.status=res.data.data.status;
						uni.setStorage('status',res.data.data.status);
				},
				fail: function(e) {
					console.log(JSON.stringify(e));
				}
			})
			console.log("dishes_id", options.dishes_id)
			if (options.dishes_id) {
				this.dishes_id = options.dishes_id;
				this.getCommentList(this.dishes_id)
			}
			if (options.indent_id) {
				this.indent_id = options.indent_id;
				this.getCommentList(this.indent_id)
			}
			if (options.dishes_id && !options.indent_id) {
				// this.isHasOrder = false;
				this.isHasOrder = true;
			}
		},
		mounted() {
			// this.getCommentList();
		},
		filters: {
			formatTime: function(time) { //
				let date = new Date(time * 1000);
				let year = date.getFullYear();
				let month = (date.getMonth() + 1).toString().padStart(2, '0');
				let day = date.getDate().toString().padStart(2, '0');
				let hour = date.getHours().toString().padStart(2, '0');
				let minute = date.getMinutes().toString().padStart(2, '0');
				let second = date.getSeconds().toString().padStart(2, '0');
				return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
			}
		},
		methods: {
			show_data() {
				// this.getCommentList();
			},
			change(e) {
				this.currentTab = e.index
			},
			changedd: function(e) {
				this.index = e.index;
				this.current = e.index
				console.log(e)
			},
			ViewImage(urls, index) {
				const getimg = this.getimg;
				let arr = [];
				for (let k in urls) {
					arr.push(getimg + urls[k]);
				}

				uni.previewImage({
					urls: arr,
					current: index
				});
			},
			// 显示评论
			showComment() {
				this.$refs.ygcComment.toggleMask('show');
			},
			// 评论事件
			pubComment(data) {
				console.log('openId', uni.getStorageSync('openid'))
				if(uni.getStorageSync('openid')){
					data.open_id = uni.getStorageSync('openid');
					data.dishes_id = this.dishes_id;
					console.log("评论事件", data)
					this.addComment(data);
				}else{
					uni.showModal({
						title: '尚未登录',
						content: '是否要前往登录页面',
						success: function(res) {
							if (res.confirm) { //这里是点击了确定以后
								// console.log('用户点击确定')
								uni.reLaunch({
									url:'../my/my'
								})
								// uni.navigateTo({
								// 	url:'../my/my'
								// })
							} else { //这里是点击了取消以后
								// console.log('用户点击取消')
							}
						}
					})
				}
			},
			// 获取某个菜品评论列表
			getCommentList(dishesId) {
				const _this = this;
				uni.request({
					url: 'https://apiorder.wzq0307.com/api/Comment/list',
					method: 'GET',
					data: {
						dishes_id: dishesId
					},
					success: (res) => {
						console.log("评论接口", res);
						if (res.data.status == 0) {
							console.log("1211111", res)
							_this.commentList = res.data.data.commentList
							if (_this.commentList.length > 0) {
								_this.list_empty = false;
							} else {
								_this.list_empty = true;
							}
						} else {
							uni.showLoading({
								title: '获取评论列表失败'
							});
							setTimeout(function() {
								uni.hideLoading();
							}, 2000);
						}
					},
					fail: function(e) {
						console.log(JSON.stringify(e));
					}
				})
			},
			addComment: function(data) {
				let _this = this;
				uni.request({
					url: 'https://apiorder.wzq0307.com/api/Comment/add/',
					method: 'GET',
					data: {
						dishes_id: data.dishes_id,
						open_id: data.open_id,
						content: data.content,
						speed_rate: data.speedRate,
						taste_rate: data.tasteRate,
						environment_rate: data.environmentRate
					},
					success: (res) => {
						console.log('00000', res);
						if (res.data.status == 0) {
							_this.commentList.unshift(res.data.data.comment);
							uni.showToast({
								title: '评论成功'
							})
							if (_this.commentList.length > 0) {
								_this.list_empty = false;
							} else {
								_this.list_empty = true;
							}
							_this.$refs.ygcComment.toggleMask('none')
						} else {
							uni.showToast({
								title: '评论失败',
								icon: 'none'
							})
						}
					},
					fail: function(e) {
						console.log(JSON.stringify(e));
					}

				})
			}
		},
	}
</script>

<style lang="less">
	.evaluate {
		background-color: #f0f0f0;
		min-height: calc(100vh - var(--status-bar-height) - 90rpx);
		font-size: 14px;
		padding-bottom: 80rpx;

		.zhpf {
			background-color: #fff;
			display: flex;
			justify-content: space-between;
			padding: 10px;
		}

		.zhpf_l {
			display: flex;
		}

		.zhpj_l_01 {
			padding: 5px 10px 0 0;
		}

		.zhpf_r {
			color: #FFB91E;
			padding-top: 5px;
		}

		.H10 {
			height: 10px;
		}

		.btn {
			padding: 0 10px 10px;
			background-color: #fff;
			display: flex;
		}

		.btn_1 {
			padding-right: 10px;
		}

		.con {
			background-color: #fff;
			padding: 10px;
		}

		.con_1 {
			display: flex;
			justify-content: space-between;
		}

		.con_1_l {
			display: flex;
			line-height: 20px;
		}

		.con_1_l img {
			height: 40px;
			width: 40px;
			border-radius: 30px;
			margin-right: 10px;
		}

		.name {
			font-size: 16px;

			span {
				padding-left: 5px;
			}
		}

		.con_1_r {
			color: #B3B3B3;
			font-size: 12px;
		}

		.ping {
			padding-top: 5px;
		}

		.con_2 {
			padding: 5px 0px 10px 50px;

			.img {
				padding-top: 8px;

				img {
					width: 24vw;
					height: 19vw;
					border-radius: 5px;
					margin: 0 2vw 10px 0;
				}
			}
		}

		.fixed-btn {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 100rpx;
			background-color: white;
			padding: 20rpx 40rpx;

			.submit-pl {
				background-color: #5F81EE;
				width: 100%;
				height: 100%;
			}
		}
	}
</style>
